/**
	Pulsar engine. Additional include file.
	Additional include file. Standard logging handlers

	Copyright (C) 2013-2014 Dmitry Dolgov

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
*/

namespace pl
{

/*
	Common log class (useful for some log subscribers)
*/
class LogTracerCommon : public ILogTracer
{
public:
	/// Ctor
	LogTracerCommon(const pl_char* formatInt, const pl_char* formatExt);

	/// Set custon output format
	void setCustomFormat(bool extFile, LogLevel level, const pl_char* format);

	/// Get custon output format
	const pl_char* _getDefaultFormat(const pl::LogData &params);

	/// Prepare output
	static void _formatLog(const LogData& params, pl::String<1024>& strOutput, const pl_char* format);

private:
	const pl_char* _formatDefaultInt;
	const pl_char* _formatDefaultExt;
	pl::Array<const pl_char*, _LOG_LEVEL_MAX> _formatCustomInt;
	pl::Array<const pl_char*, _LOG_LEVEL_MAX> _formatCustomExt;
};

/*
	Log messages to file
*/
class LogTracerFile : public LogTracerCommon
{
public:
	/// ctor
	explicit LogTracerFile(const Filepath& fileName, const pl_char* formatInt = NULL, const pl_char* formatExt = NULL);

	/// dtor
	virtual ~LogTracerFile();

protected:
	pl::StreamWrite* _fileHandle;

private:
	DECLARE_ON_LOG_MESSAGE();
	DECLARE_ON_LOG_FLUSH();

	pl::CriticalSection _cs;
};

/*
	Log messages to stdout
*/
class LogTracerPrintf : public LogTracerCommon
{
public:
	/// ctor
	LogTracerPrintf(const pl_char* formatInt = NULL, const pl_char* formatExt = NULL);

private:
	DECLARE_ON_LOG_MESSAGE();
	DECLARE_ON_LOG_FLUSH();

	pl::CriticalSection _cs;
};

/*
	Log output to in-memory queue
*/
class LogTracerMemory : public LogTracerCommon
{
public:
	/// ctor
	LogTracerMemory(int bufSize, const pl_char* formatInt = NULL, const pl_char* formatExt = NULL);

	/// Enable/disable protocol
	inline void setEnableMode(bool fEnabled)
	{
		_enableFlag = fEnabled;
	}

	inline QueueBase& getMemoryQueue()
	{
		return _q;
	}

private:
	DECLARE_ON_LOG_MESSAGE();
	QueueBase _q;
	bool _enableFlag;
};

} // namespace pl

//
// End of file 'pulsar_log.h'
//
